// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties.controllers;

import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.external.AbstractCompNodeExt;
import org.dengues.core.process.ICompGefNode;
import org.dengues.core.process.ICompNode;
import org.dengues.designer.ui.properties.PropertyTabDynamicSection;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-4-16 qiang.zhang $
 * 
 */
public class ExternalPropertyController extends AbstractPropertyController {

    private SelectionAdapter btnListener = new SelectionAdapter() {

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        @Override
        public void widgetSelected(SelectionEvent e) {
            ICompNode node = (ICompNode) dps.getElem();
            if (node.getComponent().isExternalNode()) {
                AbstractCompNodeExt compNodeExt = ((ICompGefNode) node).getExternalNode(true);
                if (compNodeExt != null) {
                    compNodeExt.openShell();
                }
            }
        }
    };

    /**
     * Qiang.Zhang.Adolf@gmail.com ExternalPropertyController constructor comment.
     * 
     * @param dps
     */
    public ExternalPropertyController(PropertyTabDynamicSection dps) {
        super(dps);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.properties.controllers.AbstractPropertyController#createControl(org.eclipse.swt.widgets.Composite,
     * org.dengues.core.components.NodePropertyParameter, org.eclipse.swt.widgets.Control)
     */
    @Override
    public Control createControl(Composite composite, NodePropertyParameter param, Control lastControl) {
        Composite subcomposite = (Composite) super.createControl(composite, param, lastControl);
        Button button;
        CLabel cLabel = getWidgetFactory().createCLabel(subcomposite, param.getDisplayName());
        GridData gridData = new GridData();
        gridData.widthHint = STANDARD_LABEL;
        cLabel.setLayoutData(gridData);
        Button text = null;
        text = getWidgetFactory().createButton(subcomposite, "...", SWT.NONE); //$NON-NLS-1$
        ControlDecoration controlDecoration = new ControlDecoration(text, SWT.RIGHT | SWT.TOP);
        if (param.isRequired()) {
            FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
                    FieldDecorationRegistry.DEC_REQUIRED);
            controlDecoration.setImage(fieldDecoration.getImage());
            controlDecoration.setDescriptionText(fieldDecoration.getDescription());
        }
        button = (Button) controlDecoration.getControl();
        if (param.getValue() != null) {
            button.setText(param.getValue().toString());
        }
        button.addSelectionListener(btnListener);
        GridData data = new GridData();
        data.heightHint = BUTTON_HEIGHT;
        data.widthHint = BUTTON_HEIGHT;
        data.horizontalIndent = FieldDecorationRegistry.getDefault().getMaximumDecorationWidth();
        button.setLayoutData(data);
        button.setData(PARAMETER_NAME, param.getName());
        curControlMap.put(param.getName(), button);
        return button;
    }

}
